package com.viu.technology.config.redis;

import java.time.Duration;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableCaching
@EnableTransactionManagement
public class RedisConfiguration extends CachingConfigurerSupport {

	/**
	 * 使用fastjson序列化
	 */
	@Bean
	public RedisSerializer fastJson2JsonRedisSerializer() {
		return new FastJsonRedisSerializer<Object>(Object.class);
	}

	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory factory, RedisSerializer fastJson2JsonRedisSerializer) {
		StringRedisTemplate template = new StringRedisTemplate(factory);

		template.setValueSerializer(fastJson2JsonRedisSerializer);
		// 开启事务支持
		template.setEnableTransactionSupport(true);
		template.afterPropertiesSet();
		return template;
	}

	@Bean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory,
			RedisSerializer fastJson2JsonRedisSerializer) {
		StringRedisTemplate template = new StringRedisTemplate(factory);

		template.setValueSerializer(fastJson2JsonRedisSerializer);
		// 开启事务支持
		template.setEnableTransactionSupport(true);
		template.afterPropertiesSet();
		return template;
	}

	/**
	 * 替换Redis Cache方案使用JDK序列化方式为FastJson序列化
	 */
	@Bean
	@Primary
	public RedisCacheConfiguration redisCacheConfiguration() {
		FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
		RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
		configuration = configuration.serializeValuesWith(
				RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer));
		configuration.entryTtl(Duration.ofDays(30));
		return configuration;
	}

	@Bean
	public LettuceConnectionFactory masterSource() {
		// 哨兵模式
		/*
		 * RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
		 * .master("mymaster") .sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1",
		 * 26380); return new LettuceConnectionFactory(sentinelConfig);
		 */
		return new LettuceConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379));
	}

	/*
	 * @Bean public RedisCacheManager cacheManager(RedisConnectionFactory
	 * connectionFactory) { return RedisCacheManager.create(connectionFactory); }
	 */

}
